<?php
/**
 * Created by PhpStorm.
 * Author: D H
 * Date: 2017/6/16
 * Time: 17:26
 */
namespace app\api\logic;
use think\Db;
use tool\Common;

class Product extends Base{

    //根据品牌得到产品列表
    public function getProductListByBrandID(){
        //测试数据
       /* $arr = ' {
          "brandID":7
        }';
        $data = json_decode($arr,true);*/

       $data = $this->data;
        $list = Db::view('product_sell',['id' => 'productID','name' => 'productName'])
            ->where('brandID',$data['brandID'])
            ->select();
        return  Common::rm(1,'操作成功',['productList' => $list]);

    }
    //根据产品ID得到产品详情
    public function getProductByProductID(){
        //测试数据
      /*  $arr = '{
           "productID": 4
        }';
        $data = json_decode($arr,true);*/
        $data = $this->data;
        $list = Db::view('product_sell',['name' => 'productName','thumb','field','introduce','catID'])
            ->view('product_content','content','product_sell.id = product_content.productID')
            ->view('company_sell','certificate','company_sell.productID = product_sell.id')
            ->where('product_sell.id',$data['productID'])
            ->find();
        $new_in = json_decode($list['introduce'],true);
        $new_cn = json_decode($list['content'],true);

        $list['MethodUse'] = $new_cn['MethodUse'];
        $list['MethodUseImg'] = $new_cn['MethodUseImg'];
        $list['upkeepUse'] = $new_cn['upkeepUse'];
        $list['upkeepUseImg'] = $new_cn['upkeepUseImg'];
        $list['introduce'] = $new_in;

        $new_list = Db::view('product_sell',['id' => 'productID','name' => 'productName'])
            ->where('product_sell.catID',$list['catID'])
            ->limit(3)
            ->select();
        $_list =[];
        foreach ($new_list as $k => $val){
            if ($val['productID'] !== $data['productID']){
                array_push($_list,$val);
            }
        }
        if (count($_list) > 2){
            array_pop($_list);
        }
        $list['similarList'] = $_list;
        unset($list['catID']);
        unset($list['content']);
        return Common::rm(1,'操作成功',['result' => [$list]]);

    }
    //产品对比
    public function contrastProduct(){
        //测试数据
       /* $arr = '{
          "productIDS": [
               1,
               2
          ]
        }';
        $data = json_decode($arr,true);*/
       $data = $this->data;
        if(count($data['productIDS']) > 2){
            return Common::rm(-2,'一次只能对比两件装备');
        };
        if (count($data['productIDS']) < 2){
            return Common::rm(-2,'对比必须是两件装备');
        }
        //判断所选装备是否为同一类产品
        $judge = Db::view('product_sell','catID')->where(['id' => ['in',$data['productIDS']]])->select();
        if ($judge[0]['catID'] !== $judge[1]['catID']){
            return Common::rm(-2,'请选择同类型的两种产品');
        }
        //得到对比信息
        $list = Db::view('product_sell',['id' =>'productID','name' =>'productName','thumb'])
            ->view('product_parameter',['name' => 'parameterName','val' =>'parameterVal'],'product_parameter.productID = product_sell.id')
            ->where(['productID' =>['in',$data['productIDS']]])
            ->select();
        $new_list = [];
        foreach ($list as $k => $val){
            $new_list[$val['productID']][] = $val;
        }
        $_new_list = [];
        foreach ($new_list as $k => $val){
            $_new_list[$k]['productID'] =  $val[0]['productID'];
            $_new_list[$k]['productName'] =  $val[0]['productName'];
            $_new_list[$k]['thumb'] =  $val[0]['thumb'];
            foreach ($val as $key => $value){
                $_new_list[$k]['parameterList'][$key][] = $value['parameterName'];
                $_new_list[$k]['parameterList'][$key][] = $value['parameterVal'];
            }
        }
        return Common::rm(1,'操作成功',['allParameterList' => array_reverse($_new_list)]);
    }
    //添加对比产品
    public function addContrast(){
      //测试数据
     /* $arr = '{
        "productID": 2,
        "userID": 3
      }';
      $data = json_decode($arr,true);*/
      $data = $this->data;
      $exist = Db::name('product_contrast')
            ->where('productID',$data['productID'])
            ->where('userID',$data['userID'])
            ->find();
      if ($exist){
            return Common::rm(-2,'请勿重复添加');
        }
      Db::name('product_contrast')->insertGetId($data);
      return Common::rm(1,'操作成功');
    }
    //得到对比装备列表
    public function getContrastList(){
        //测试数据     登录以后直接获取userID
      /*  $arr = '{
             "userID":3
        }';
        $user = json_decode($arr,true);*/

        $list = Db::view('product_contrast','productID')
            ->view('product_sell',['name' => 'productName','field','thumb'],'product_sell.id = product_contrast.productID')
            ->where('product_contrast.userID',$this->getUserID())
            ->select();
        return Common::rm(1,'操作成功',['result' =>$list]);
    }
}